package cur.nature.starter.controller;

import cur.nature.admin.common.MessageUtil;
import cur.nature.framework.core.exception.BizException;
import cur.nature.framework.core.log.LOG;
import cur.nature.framework.core.mirco.MircoIdUtil;
import cur.nature.framework.core.util.IdUtil;
import cur.nature.framework.core.util.SnowflakeIdUtil;
import cur.nature.framework.mvc.base.BaseAPI;
import cur.nature.framework.mvc.crud.CrudController;
import cur.nature.starter.model.Manager_DictVO;
import cur.nature.starter.service.Manager_DictService;
import cur.nature.tinyorm.core.bean.PageBean;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 通用字典表 Controller
 * 作者：Auto Generator By wujian
 * 生成日期：2023-07-09 16:14:19
 */
@Tag(name = "通用字典 接口")
@RestController
@RequestMapping("manager/v1/api/Admin_Dict")
public class Manager_DictController extends CrudController<Manager_DictService> {
    public Manager_DictController(Manager_DictService baseService) {
        super(baseService);
    }

//    @Autowired
//    private CachedUidGenerator uidGenerator;

    @Operation(summary = "查询 通用字典表")
    @PostMapping("/list")
    public PageBean<Manager_DictVO> list(@Valid @RequestBody Manager_DictVO.list_Param param) throws Exception {
        PageBean<Manager_DictVO> list = getService().list(param);
        try {
            LOG.info("id: " + IdUtil.generate());
            LOG.info("id2: " + MircoIdUtil.generateId());
            if (list != null) {
                throw new BizException("测试一下。。。");
            }
        }catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }

        return list;
    }

    @Operation(summary = "查询 通用字典表")
    @PostMapping("/listByCond")
    public List<Manager_DictVO> listByCond(@Valid @RequestBody Manager_DictVO.list_Param param) throws Exception {
        List<Manager_DictVO> list = getService().listByCond(param);
        return list;
    }

    @Operation(summary = "更新 通用字典表")
    @PostMapping("/operate")
    public BaseAPI.SaveResult<Manager_DictVO> save(@Valid @RequestBody List<Manager_DictVO> param) throws Exception {
        int rows = getService().saveList(param);
        BaseAPI.SaveResult result = new BaseAPI.SaveResult();
        result.setRows(rows);
        result.setObject(rows == 0 ? null : param);
        return result;
    }

    /**
     * 用于测试国际化
     * @return
     */
    @Operation(summary = "测试国际化")
    @GetMapping("/testLocalize")
    public BaseAPI.SaveResult<String> testLocalize() {
        String message = MessageUtil.getMessage("greeting.message");
        BaseAPI.SaveResult result = new BaseAPI.SaveResult();
        result.setObject(message);
        return result;
    }

}

